package com.bstek.uflo.console.view.security.filter.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.bstek.dorado.view.ViewElement;
import com.bstek.dorado.view.widget.base.tab.Tab;
import com.bstek.uflo.console.view.security.filter.AbstractElementFilter;
import com.bstek.uflo.process.security.Authority;
import com.bstek.uflo.process.security.ComponentAuthority;

/**
 * @author Jacky.gao
 * @since 2013年12月9日
 */
@Component("uflo.console.tabElementFilter")
public class TabElementFilter extends AbstractElementFilter {

	public boolean support(ViewElement element) {
		return element instanceof Tab;
	}

	public void filter(ViewElement element,List<ComponentAuthority> componentAuthorities) {
		Tab tab=(Tab)element;
		boolean authority=authorityTab(tab.getName(),tab,componentAuthorities);
		if(authority)return;
		authorityTab(tab.getCaption(),tab,componentAuthorities);
	}
	
	private boolean authorityTab(String id,Tab tab,List<ComponentAuthority> componentAuthorities){
		if(StringUtils.isEmpty(id))return false;
		Authority auth=filterComponent(id,componentAuthorities);
		if(auth!=null){
			if(auth.equals(Authority.Hide)){
				tab.setIgnored(true);
			}else if(auth.equals(Authority.Read)){
				tab.setDisabled(true);
			}
			return true;
		}
		return false;
	}
}
